package com.neouton.crm.supplier.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.customer.entity.CustomerVO;
import com.neouton.crm.supplier.entity.SupplierVO;
import com.neouton.crm.supplier.service.SupplierService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: crm
 * @Timestamp: 2022-11-17
 * @Author: WuJie
 * @Description: 客户信息相关接口
 */
@RestController
@RequestMapping("supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    /**
     * 多条件分页查询客户信息列表
     *
     * @param pageNum  当前页
     * @param pageSize 每页数量
     * @param supplier 其余查询条件
     * @return 分页查询结果
     * @Timestamp: 2022-11-18
     * @Author: WuJie
     */
    @PostMapping("/getSupplierByPage/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getSupplierByPage(@PathVariable(value = "pageNum") int pageNum,
                                         @PathVariable(value = "pageSize") int pageSize,
                                         @RequestBody SupplierVO supplier, @PathVariable(value = "orderBy") String orderBy) {
        IPage<SupplierVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<SupplierVO> result = supplierService.querySupplierByPage(page, supplier, orderBy);
        return ResponseAPI.successData(result);
    }

    /**
     * 获取供应商待办列表
     * @param pageNum
     * @param pageSize
     * @param supplier
     * @param orderBy
     * @param token
     * @return
     */
    @PostMapping("/getSupplierToDoList/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getSupplierToDoList(@PathVariable(value = "pageNum") int pageNum,
                                           @PathVariable(value = "pageSize") int pageSize,
                                           @RequestBody SupplierVO supplier,@PathVariable(value = "orderBy") String orderBy,@RequestHeader("token") String token) {
        IPage<SupplierVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<SupplierVO> result = supplierService.getSupplierToDoList(page, supplier,orderBy,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 获取供应商已办列表
     * @param pageNum
     * @param pageSize
     * @param supplier
     * @param orderBy
     * @param token
     * @return
     */
    @PostMapping("/getSupplierDoneList/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getSupplierDoneList(@PathVariable(value = "pageNum") int pageNum,
                                           @PathVariable(value = "pageSize") int pageSize,
                                           @RequestBody SupplierVO supplier,@PathVariable(value = "orderBy") String orderBy,@RequestHeader("token") String token) {
        IPage<SupplierVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<SupplierVO> result = supplierService.getSupplierDoneList(page, supplier,orderBy,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 获取供应商申请记录列表
     * @param pageNum
     * @param pageSize
     * @param supplier
     * @param orderBy
     * @param token
     * @return
     */
    @PostMapping("/getSupplierRecordsList/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getSupplierRecordsList(@PathVariable(value = "pageNum") int pageNum,
                                              @PathVariable(value = "pageSize") int pageSize,
                                              @RequestBody SupplierVO supplier,@PathVariable(value = "orderBy") String orderBy,@RequestHeader("token") String token) {
        IPage<SupplierVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<SupplierVO> result = supplierService.getSupplierRecordsList(page, supplier,orderBy,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 新增或更新客户信息
     *
     * @param supplier 需更新实体
     * @return 成功失败
     * @Timestamp: 2022-11-22
     * @Author: WuJie
     */
    @PostMapping("/saveOrUpdateSupplier")
    public ResponseAPI saveOrUpdateSupplier(@RequestBody SupplierVO supplier,@RequestHeader("token") String token) {
        Boolean data = supplierService.saveOrUpdateSupplier(supplier,token);
         return ResponseAPI.successData(data);
    }

    /**
     * 审批通过
     * @param supplier
     * @param token
     * @return
     */
    @PostMapping("/approveSupplier")
    public ResponseAPI approveSupplier(@RequestBody SupplierVO supplier,@RequestHeader("token") String token) {
        Boolean data = supplierService.approveSupplier(supplier,token,false);
        return ResponseAPI.successData(data);
    }

    /**
     * 审批驳回
     * @param supplier
     * @param token
     * @return
     */
    @PostMapping("/rejectionSupplier")
    public ResponseAPI rejectionSupplier(@RequestBody SupplierVO supplier,@RequestHeader("token") String token) {
        Boolean data = supplierService.rejectionSupplier(supplier,token);
        return ResponseAPI.successData(data);
    }

    /**
     * 禁用信息
     * @param supplierId 供应商ID
     * @return 成功失败
     */
    @PostMapping("/deleteSupplier/{supplierId}")
    public ResponseAPI deleteSupplier(@PathVariable(value = "supplierId") Integer supplierId) {
        Boolean state = supplierService.deleteSupplierById(supplierId);
        return ResponseAPI.successData(state);
    }

    /**
     * 通过审批日志ID改变客户状态：禁用客户信息
     * @param approvalId
     * @return
     */
    @PostMapping("/forbiddenCustomerByApplyId/{approvalId}")
    public ResponseAPI forbiddenCustomerByApplyId(@PathVariable(value = "approvalId") Integer approvalId){
        Boolean flage = supplierService.forbiddenCustomerByApplyId(approvalId);
        return ResponseAPI.successData(flage);
    }


    /**
     * 启用信息
     * @param supplierId 供应商ID
     * @return 成功失败
     */
    @PostMapping("/enableSupplier/{supplierId}")
    public ResponseAPI enableSupplier(@PathVariable(value = "supplierId") Integer supplierId) {
        Boolean flage = supplierService.enableSupplierById(supplierId);
        return ResponseAPI.successData(flage);
    }

    @PostMapping("/queryTodoSupplierByApprovalId/{approvalId}")
    public ResponseAPI queryTodoSupplierByApprovalId(@PathVariable("approvalId") Integer approvalId){
        return ResponseAPI.successData( supplierService.queryTodoSupplierByApprovalId(approvalId));
    }

    @PostMapping("/queryTHistorySupplierByApprovalId/{approvalId}")
    public ResponseAPI queryTHistorySupplierByApprovalId(@PathVariable("approvalId") Integer approvalId){
        return ResponseAPI.successData( supplierService.queryTHistorySupplierByApprovalId(approvalId));
    }
    /**
     * 根据客户名称查询客户信息是否存在
     * @param sp
     * @return
     */
    @PostMapping("/querySupplier")
    public ResponseAPI querySupplier(@RequestBody SupplierVO sp) {
        return ResponseAPI.successData(supplierService.querySupplier(sp));
    }
    /**
     * 查询并更改是否为客户，每次刷新更新
     * @return true成功，false失败
     */
    @PostMapping("/selectIsCustomer")
    public ResponseAPI selectIsCustomer() {
        Boolean flage = supplierService.selectIsCustomer();
        return ResponseAPI.successData(flage);
    }

    /**
     * 查询所有的供应商名称
     * @return
     */
    @PostMapping("/selectAllEnterprise")
    public ResponseAPI selectAllEnterprise() {
        List<SupplierVO> sp = supplierService.selectAllEnterprise();
        return ResponseAPI.successData(sp);
    }

    /**
     * 查询所有通过并且启用的供应商名称
     * @return
     */
    @PostMapping("/selectAllSupplier")
    public ResponseAPI selectAllSupplier() {
        List<SupplierVO> ct = supplierService.selectAllSupplier();
        return ResponseAPI.successData(ct);
    }
    @PostMapping("/exportSupplierInfo")
    public void supplierInfosExport(HttpServletResponse response, @RequestBody SupplierVO supplier) throws Exception{
        supplierService.exportSupplierInfo(response,supplier);
    }

    /**
     * 依据供应商ID获取供应商相关信息
     * @param supplierId
     * @return
     */
    @PostMapping("/querySupplierCorrelationBySupplierID/{supplierId}")
    public ResponseAPI queryProjectByID(@PathVariable(value = "supplierId") int supplierId,@RequestHeader("token") String token) {
        SupplierVO supplierCorrelation = supplierService.querySupplierCorrelationBySupplierID(supplierId,token);
        return ResponseAPI.successData(supplierCorrelation);
    }

    /**
     * 通过供应商ID查询供应商视图信息接口
     * @param supplierId
     * @param token
     * @return
     */
    @PostMapping("/getSupplierDocumentaryViewInfo/{supplierId}")
    public ResponseAPI getSupplierDocumentaryViewInfo(@PathVariable(value = "supplierId") Integer supplierId,@RequestHeader("token") String token) {
        return ResponseAPI.successData(supplierService.getSupplierDocumentaryViewInfo(supplierId,token));
    }
}
